﻿//Copyright (c) Chris Pietschmann 2013 (http://pietschsoft.com)
//Licensed under the GNU Library General Public License (LGPL)
//License can be found here: http://easypatterns.codeplex.com/license

namespace EasyPatterns.Factory
{
    /// <summary>
    /// Single parameter based Factory Pattern interface.
    /// </summary>
    public interface IParameterFactory
    {
        object Create(object parameter);
    }

    /// <summary>
    /// Single parameter based Factory Pattern interface.
    /// </summary>
    /// <typeparam name="TReturnType">The Type that the Factory will Return.</typeparam>
    /// <typeparam name="TParameterType">The Type of the Parameter that the Create method accepts.</typeparam>
    public interface IParameterFactory<TReturnType, TParameterType> : IParameterFactory
    {
        TReturnType Create(TParameterType parameterValue);
    }

    /// <summary>
    /// A Factory Pattern base class that accepts a single parameter in its Create method.
    /// </summary>
    /// <typeparam name="TReturnType"></typeparam>
    /// <typeparam name="TParameterType"></typeparam>
    public abstract class ParameterFactory<TReturnType, TParameterType> : IParameterFactory<TReturnType, TParameterType>
    {
        protected abstract TReturnType CreateInstance(TParameterType parameterValue);

        public TReturnType Create(TParameterType parameterValue)
        {
            return this.CreateInstance(parameterValue);
        }

        public object Create(object parameter)
        {
            return this.Create((TParameterType)parameter);
        }
    }
}
